import './public-path'
import Vue from 'vue'

// 导入重置样式和ele样式
import 'normalize.css/normalize.css'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/reset.css'
import 'element-ui/lib/theme-chalk/index.css'

// import 'babel-polyfill'
import {
  checkMinor
} from 'common/src/utils/common'

// 导入自定义样式
import 'common/src/styles/index.scss'

import App from './App.vue'
import routes from './router'
import VueRouter from 'vue-router'
import { globalStore, fetch } from 'common/src/index'
import store from './store'
import utils from 'common/src/utils/utils'
import SysDirective from 'common/src/directives'
import SysComponent from 'common/src/components/common'

// 导入第三方组件和国际化
import i18n from 'common/src/i18n'

// Vue.prototype.$echarts = echarts // see webpack.base.conf
// Vue.prototype._ = _ // see webpack.base.conf
// Vue.prototype.$moment = moment // see webpack.base.conf

Vue.prototype.$utils = utils
utils.i18n = (key, value) => i18n.t(key, value)
Vue.prototype.$checkMinor = checkMinor

Vue.config.productionTip = false

Vue.use(ElementUI, {
  size: 'small', // set element-ui default size
  i18n: (key, value) => i18n.t(key, value)
})

Vue.use(SysDirective)
Vue.use(SysComponent)

let VM = null

function render (props = {}) {
  const { container, routerBase } = props
  const router = new VueRouter({
    base: window.__POWERED_BY_QIANKUN__ ? routerBase : process.env.BASE_URL,
    mode: 'history',
    routes
  })

  // function isLogin (user) {
  //   return Boolean(user && user.auth)
  // }

  router.beforeEach((to, from, next) => {
    // console.log('------------------child------------------')
    // console.log(to);

    // if (isLogin(store.state.global.user)) {
    //   next()
    // } else {
    //   history.pushState(null, '/', '/login')
    // }

    // if (to.meta.auth) {
    //   if (isLogin(store.state.global.user)) {
    //     next()
    //   } else {
    //     history.pushState(null, '/', '/login')
    //   }
    // } else {
    //   next()
    // }

    next()
  })

  // router.afterEach((to, from) => {
  //   // console.log('afterEach');
  //   // NProgress.done()
  // })

  VM = new Vue({
    router,
    store,
    i18n,
    render: (h) => h(App)
  })
  fetch.initFun(VM, router)
  VM.$mount(container ? container.querySelector('#app') : '#app')
}

if (!window.__POWERED_BY_QIANKUN__) {
  // 这里是子应用独立运行的环境，实现子应用的登录逻辑

  // 独立运行时，也注册一个名为global的store module
  globalStore.globalRegister(store)
  // 模拟登录后，存储用户信息到global module
  // const userInfo = { name: '我是独立运行时名字叫张三' } // 假设登录后取到的用户信息
  // store.commit('global/setGlobalState', { user: userInfo, mockUser: {} })

  render()
}

export async function bootstrap () {
  console.log('[vue] vue app bootstraped')
}

export async function mount (props) {
  // console.log('[vue] props from main framework', props)
  // console.log(store.state)
  // console.log(props)
  globalStore.globalRegister(store, props)

  render(props)
}

export async function unmount () {
  VM.$destroy()
  VM.$el.innerHTML = ''
  VM = null
}
